﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using EmoneyWeiXinThreadBLL;

namespace EmoneyWeiXin.Controllers
{
    public class StockDoctorlogController : Controller
    {
        //
        // GET: /StockDoctorlog/
        public ActionResult Index()
        {
            string weixinid = string.IsNullOrEmpty(Request.QueryString["weixinid"]) ? "1" : Request.QueryString["weixinid"];
            ViewBag.weixinid = weixinid;
            return View();
        }

        public ActionResult Index_Data()
        {
            string weixinid = Request.QueryString["weixinid"];
            string nickname = Request.QueryString["nickname"];
            string starttime = Request.QueryString["starttime"];
            string endtime = Request.QueryString["endtime"];
            string sex = Request.QueryString["sex"];
            string sf = Request.QueryString["sf"];
            string dq = Request.QueryString["dq"];
            string groupid = Request.QueryString["groupid"];
            int pageindex = string.IsNullOrEmpty(Request.QueryString["pageindex"]) ? 1 : Convert.ToInt32(Request.QueryString["pageindex"]);
            StockDoctorlogBLL bll = new StockDoctorlogBLL();
            ViewBag.queryStock = bll.getStockDoctorlogContentALL(weixinid, nickname, starttime, endtime, sex, sf, dq, groupid, pageindex, 15);
            ViewBag.weixinid = weixinid;
            return View();
        }

        public int Index_dataNum()
        {
            string weixinid = Request.QueryString["weixinid"];
            string nickname = Request.QueryString["nickname"];
            string starttime = Request.QueryString["starttime"];
            string endtime = Request.QueryString["endtime"];
            string sex = Request.QueryString["sex"];
            string sf = Request.QueryString["sf"];
            string dq = Request.QueryString["dq"];
            string groupid = Request.QueryString["groupid"];
            StockDoctorlogBLL bll = new StockDoctorlogBLL();
            return bll.getStockDoctorlogContentCount(weixinid, nickname, starttime, endtime, sex, sf, dq, groupid);
        }

        public int Index_dataaddNum()
        {
            string weixinid = string.IsNullOrEmpty(Request.QueryString["weixinid"]) ? "1" : Request.QueryString["weixinid"];
            string openid = string.IsNullOrEmpty(Request.QueryString["openid"]) ? "ohdkZt6szjqrI8s255oSBpPMJiDk" : Request.QueryString["openid"];
            StockDoctorlogBLL bll = new StockDoctorlogBLL();
            return bll.getStockDoctorlogAddNum(weixinid, openid);
        }

        public ActionResult Index_add()
        {
            string weixinid = string.IsNullOrEmpty(Request.QueryString["weixinid"]) ? "1" : Request.QueryString["weixinid"];
            string openid = string.IsNullOrEmpty(Request.QueryString["openid"]) ? "ohdkZt6szjqrI8s255oSBpPMJiDk" : Request.QueryString["openid"];
            string sid = string.IsNullOrEmpty(Request.QueryString["sid"]) ? "123" : Request.QueryString["sid"];
            StockDoctorlogBLL bll = new StockDoctorlogBLL();
            ViewBag.Nickname = bll.getNickName(weixinid, openid);
            ViewBag.openid = openid;
            ViewBag.weixinid = weixinid;
            ViewBag.sid = sid;

            return View();
        }

        public ActionResult Index_addData()
        {
            string weixinid = string.IsNullOrEmpty(Request.QueryString["weixinid"]) ? "1" : Request.QueryString["weixinid"];
            string openid = string.IsNullOrEmpty(Request.QueryString["openid"]) ? "ohdkZt6szjqrI8s255oSBpPMJiDk" : Request.QueryString["openid"];
            int pageindex = string.IsNullOrEmpty(Request.QueryString["pageindex"]) ? 0 : Convert.ToInt32(Request.QueryString["pageindex"]);
            int pagecount = 10;
            StockDoctorlogBLL bll = new StockDoctorlogBLL();
            ViewBag.queryStockD = bll.getStockDoctorlogAddALL(weixinid, openid, pageindex, pagecount);

            MemcachedTools1 tools = new MemcachedTools1();
            string keys = StringTools.ChangeRealKey("weixinUser", weixinid);
            var obj = tools.Get(keys);
            weixinUserEntity weixinuser = new weixinUserEntity();
            if (obj != null)
            {
                weixinuser = JsonTools.ToObject<weixinUserEntity>(obj.ToString());
            }
            ViewBag.weixinuser = weixinuser;
            ViewBag.weixinid = weixinid;
            return View();
        }

        public string ajaxTuisong()
        {
            string nickname = "";
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us == null || string.IsNullOrEmpty(us.Username))
            {
                return "";
            }
            else
            {
                nickname = us.Username;
            }

            string strResult = "-1";
            string action = Request.QueryString["action"];
            string weixinid = Request.Form["weixinid"];
            string openid = Request.Form["openid"];
            string sid = Request.Form["sid"];
            string newsreportid = Request.Form["newsreportid"];
            string txtdhwb = Request.Form["txtdhwb"];
            string groupid = Request.Form["groupid"];
            string voiceid = Request.Form["voiceid"];
            string voiceContent = Request.Form["voiceContent"];
            string[] strArr = null;
            if (!string.IsNullOrEmpty(openid))
            {
                strArr = openid.Split(',');
            }
            string strtoken = "";
            string strJson = "";
            string actiondhwb = Request.Form["action"];
            MemcachedTools1 mmtools = new MemcachedTools1();

            if (ThreadWeiXinBLL.listweixinUser == null || ThreadWeiXinBLL.listweixinUser.Count < 0)
            {
                ThreadWeiXinBLL.getWeixinUser();
            }
            if (ThreadWeiXinBLL.listweixinUser != null && ThreadWeiXinBLL.listweixinUser.Count > 0)
            {
                var query = ThreadWeiXinBLL.listweixinUser.Where(x => x.id == Convert.ToInt32(weixinid)).ToList();
                if (query != null && query.Count > 0)
                {
                    if (!string.IsNullOrEmpty(query[0].AppId) && !string.IsNullOrEmpty(query[0].AppSecret))
                    {
                        strtoken = TokenHelper.GetToken(query[0].AppId, query[0].AppSecret);
                    }
                }
            }
            string openidStrc = "";
            if (action == "newsreport")
            {
                List<NewsReportEntity> list = new List<NewsReportEntity>();
                if (!string.IsNullOrEmpty(newsreportid))
                {
                    string[] strCrr = newsreportid.Split('^');
                    for (int q = 0; q < strCrr.Length; q++)
                    {
                        if (!string.IsNullOrEmpty(strCrr[q]))
                        {
                            string keys = StringTools.ChangeRealKey("newsreport", weixinid + strCrr[q]);
                            MemcachedTools1 mmtoos = new MemcachedTools1();
                            object objkeys = mmtoos.Get(keys);
                            if (objkeys != null)
                            {
                                NewsReportEntity newsreport = JsonTools.ToObject<NewsReportEntity>(objkeys.ToString());
                                if (newsreport != null && newsreport.ID > 0)
                                {
                                    list.Add(newsreport);
                                }
                            }
                        }
                    }
                }
                if (list != null && list.Count > 0)
                {
                    strJson = StringTools.GetNewsPortJson(list);
                }
                if (!string.IsNullOrEmpty(strtoken) && !string.IsNullOrEmpty(strJson))
                {

                    for (int j = 0; j < strArr.Length; j++)
                    {
                        if (!string.IsNullOrEmpty(strArr[j]) && !openidStrc.Contains(strArr[j]))
                        {
                            openidStrc += strArr[j];
                            string tojson1 = strJson.Replace("{0}", strArr[j]);
                            tojson1 = tojson1.Replace("{1}", weixinid);
                            weixinResult res = new weixinResult();
                            try
                            {
                                res = JsonTools.ToObject<weixinResult>(TokenHelper.RequestPost(Config.WeiXin_ApiUrl + "message/custom/send?access_token=" + strtoken, tojson1));
                            }
                            catch (Exception ex)
                            {
                                Response.Write(ex.Message);
                                Response.End();
                            }
                            if (res != null)
                            {
                                PropellingLogEntity pplogEntity = new PropellingLogEntity();
                                pplogEntity.id = 0;
                                pplogEntity.newsreportid = newsreportid;
                                pplogEntity.weixinid = Convert.ToInt32(weixinid);
                                pplogEntity.ToUsername = strArr[j];
                                pplogEntity.errorResult = res.errcode;
                                pplogEntity.updatetime = System.DateTime.Now;
                                pplogEntity.hisid = 3;

                                PropellingLogBLL dalWeixinuser = new PropellingLogBLL();
                                dalWeixinuser.getPropellingUpdate(pplogEntity);

                                strResult = res.errcode;

                                if (res.errcode == "0")
                                {
                                    //weixinUserBLL wexinbll = new weixinUserBLL();
                                    //wexinbll.getStockdictor(sid.Split(',')[j], newsreportid, weixinid);

                                    C_ReplyManagerBLL crebll = new C_ReplyManagerBLL();
                                    crebll.GetC_ReplyManagerBLL(sid.Split(',')[j], weixinid, newsreportid, nickname, txtdhwb, System.DateTime.Now, "");
                                }
                            }

                        }
                    }
                }
            }

            else if (action == "dhwb")
            {
                strJson =StringTools.GetSendWbJson(txtdhwb);
                if (!string.IsNullOrEmpty(strtoken) && !string.IsNullOrEmpty(strJson))
                {

                    for (int j = 0; j < strArr.Length; j++)
                    {
                        if (!string.IsNullOrEmpty(strArr[j]) && !openidStrc.Contains(strArr[j]))
                        {
                            openidStrc += strArr[j];
                            string tojson1 = strJson.Replace("{0}", strArr[j]);
                            tojson1 = tojson1.Replace("{1}", weixinid);
                            weixinResult res = JsonTools.ToObject<weixinResult>(TokenHelper.RequestPost(Config.WeiXin_ApiUrl + "message/custom/send?access_token=" + strtoken, tojson1));
                            if (res != null)
                            {
                                PropellingLogEntity pplogEntity = new PropellingLogEntity();
                                pplogEntity.id = 0;
                                pplogEntity.newsreportid = "0";
                                pplogEntity.weixinid = Convert.ToInt32(weixinid);
                                pplogEntity.ToUsername = strArr[j];
                                pplogEntity.errorResult = res.errcode;
                                pplogEntity.updatetime = System.DateTime.Now;
                                pplogEntity.hisid = 3;

                                PropellingLogBLL dalWeixinuser = new PropellingLogBLL();
                                dalWeixinuser.getPropellingUpdate(pplogEntity);

                                strResult = res.errcode;
                                if (res.errcode == "0")
                                {
                                    //weixinUserBLL wexinbll = new weixinUserBLL();
                                    //wexinbll.getStockdictor(sid.Split(',')[j], txtdhwb, weixinid);

                                    C_ReplyManagerBLL crebll = new C_ReplyManagerBLL();
                                    crebll.GetC_ReplyManagerBLL(sid.Split(',')[j], weixinid, "0", nickname, txtdhwb, System.DateTime.Now, "");

                                }
                            }
                        }

                    }
                }
            }
            return strResult;



        }

      

    }
}
